package com.geng.controller;

import com.geng.pojo.Bonus;
import com.geng.pojo.Dept;
import com.geng.service.BonusService;
import com.geng.service.DeptService;
import com.geng.utils.ImageUtils;

import lombok.extern.slf4j.Slf4j;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Controller
public class BonusController {
    @Autowired
    private BonusService bonusService;
    @Autowired
    private DeptService deptService;
    @RequestMapping("/login")
    @ResponseBody
    public Object demoString(){
        return  "Hello";
    }
    @RequestMapping("/index")
    @ResponseBody
    public Object findBonusAll(){
        List<Dept> list = deptService.findAllDept();
        for (Dept dept : list) {
			System.out.println(dept.getDname());
		}
        return list;
    }
    @RequestMapping("/dept")
    @ResponseBody
    public Object queryByDept(String dname,String loc) { 
    	log.debug("dname"+dname+"loc"+loc);
    	List<Dept> list=deptService.queryDept(dname, loc);   	
    	for (Dept dept : list) {
			log.debug("dNo"+dept.getDeptno()+"dname"+dept.getDname()+"loc"+dept.getLoc());
		}
    	return list;
    }
    
    @RequestMapping("/logins")
    public String index() {
    	return "index";
    }
    /**
     * 根据id删除dept
     * @param deptNo
     * @return
     */
    @RequestMapping("/del")
    @ResponseBody
    public Object removeByNoDept(long deptNo) {
    	System.out.println(deptNo);
    	Map<String, Object> map = new HashMap<String,Object>();
    	if(deptService.deleteDept(deptNo)) {
    		map.put("msg", "true");
    		return map;
    	}
    	map.put("msg", "false");
    	return map;
    }
    /**
     * excel表格导出
     * @param response
     */
    @RequestMapping("/getExcel")
    public void getxcel(HttpServletResponse response) {
    	//获取dept所有信息
    	List<Dept> deptList = deptService.findAllDept();
    	for (Dept dept : deptList) {
			System.out.println(dept.getDeptno());
		}
    	//生成excel工作簿对象
    	HSSFWorkbook wb = new HSSFWorkbook();
    	//创建sheet对象并指定工作簿名字
    	HSSFSheet st = wb.createSheet("dept对象");
    	//创建行对象
    	HSSFRow row = null;
    	//第0行
    	row=st.createRow(0);   	
    	//将第一列的单元格进行跨列操作（从第0列开始跨到第2列）
        CellRangeAddress rowRegion = new CellRangeAddress(0,0,0,2);
        st.addMergedRegion(rowRegion);
        //设置行高
        row.setHeight((short)(50*8));
        row.createCell(0).setCellValue("部门信息详情");
        //设置样式
        row.getCell(0).setCellStyle(getStyle(wb, 0));
        /*CellRangeAddress columnRegion = new CellRangeAddress(0,3,0,0);
        st.addMergedRegion(columnRegion);*/
        
        //第1行
        row = st.createRow(1);
        row.createCell(0).setCellValue("部门编号");
        row.createCell(1).setCellValue("部门名称");
        row.createCell(2).setCellValue("部门loc");
        //设置字体
        for(int i = 0;i < 3;i++){
            row.getCell(i).setCellStyle(getStyle(wb,1));
        }
        
        for (int i = 0; i < deptList.size(); i++) {
			row = st.createRow(i+2);
			Dept dept = deptList.get(i);
			row.createCell(0).setCellValue(dept.getDeptno());
			row.createCell(1).setCellValue(dept.getDname());
			row.createCell(2).setCellValue(dept.getLoc());
			for (int j = 0; j < 3; j++) {
				row.getCell(j).setCellStyle(getStyle(wb, 2));
			}
			
		}
        //设置默认行高
        st.setDefaultRowHeight((short)(16.5*20));
        //列宽自适应
        for(int i=0;i<=13;i++){
            st.autoSizeColumn(i);
        }
 
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        OutputStream os;
		try {
			os = response.getOutputStream();
			//设置文件名
			response.setHeader("Content-disposition","attachment; filename=details.xls"); 
			wb.write(os);
	        os.flush();	
	        os.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}        
    }
    
    /**
     * 获取样式
     * @param hssfWorkbook
     * @param styleNum
     * @return
     */
    public HSSFCellStyle getStyle(HSSFWorkbook hssfWorkbook, Integer styleNum){
        HSSFCellStyle style = hssfWorkbook.createCellStyle();
        style.setBorderRight(BorderStyle.THIN);//右边框
        style.setBorderBottom(BorderStyle.THIN);//下边框
 
        HSSFFont font = hssfWorkbook.createFont();
        font.setFontName("微软雅黑");//设置字体为微软雅黑
 
        HSSFPalette palette = hssfWorkbook.getCustomPalette();//拿到palette颜色板,可以根据需要设置颜色
        switch (styleNum){
            case(0):{
                style.setAlignment(HorizontalAlignment.CENTER_SELECTION);//跨列居中
                font.setBold(true);//粗体
                font.setFontHeightInPoints((short) 14);//字体大小
                style.setFont(font);
                palette.setColorAtIndex(HSSFColor.BLUE.index,(byte)184,(byte)204,(byte)228);//替换颜色板中的颜色
                style.setFillForegroundColor(HSSFColor.BLUE.index);
                style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            }
            break;
            case(1):{
                font.setBold(true);//粗体
                font.setFontHeightInPoints((short) 11);//字体大小
                style.setFont(font);
            }
            break;
            case(2):{
                font.setFontHeightInPoints((short)10);
                style.setFont(font);
            }
            break;
            case(3):{
                style.setFont(font);
 
                palette.setColorAtIndex(HSSFColor.GREEN.index,(byte)0,(byte)32,(byte)96);//替换颜色板中的颜色
                style.setFillForegroundColor(HSSFColor.GREEN.index);
                style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            }
            break;
        }
 
        return style;
    }
    /**
     * 跳转到验证码图片的页面
     * @return
     */
    @RequestMapping("/toImg")
    public String toImgs() {
    	return "imgs";
    }
    /**
     * 加载图片
     * @param response
     * @param request
     */
    @RequestMapping("/imgLoad")
    public void imgLoad(HttpServletResponse response,HttpServletRequest request) {
    	ImageUtils.genValidateCode(request, response);
    	HttpSession session = request.getSession();
    	System.out.println("验证码："+session.getAttribute("validateCode"));
    }
    
}
